<?php
/**
 * Marco Mounzer
 * m.mounzer@gmail.com
 * @copyright 2013
 */
require_once 'lib/begin.php';
require_once 'WebCar.php';

$obj = new WebCar();
$title = 'Automoda Fucecchio - Veicoli';

if (isset($_REQUEST['id'])) {
    $obj = Database::GetIstance()->loadObject($obj, $_REQUEST['id']);
    $title = 'Automoda Fucecchio - ' . ucfirst($obj->marca->nome) . ' ' . ucfirst($obj->modello);
    $description = ucfirst($obj->marca->nome) . ' ' . ucfirst($obj->modello) . ' ' . nl2br($obj->note);
}

require_once '_header.php';

if ($obj === false) {
    echo "Nessun dato trovato.";
    require_once '_footer.php';
    die;
}

$img = $obj->photo->Id . '.' . $obj->photo->extension;
if (file_exists($uploaddir . $img))
    $img = '/images/cars/' . $img;
else
    $img = '/images/automoda.png';
?>
<table style="width: 800px;">
    <thead>
        <tr>
            <th colspan="3"><?php echo ucfirst($obj->marca->nome) . ' ' . ucfirst($obj->modello); ?></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th colspan="2" style="width: 100%;">Caratteristiche tecniche</th>
            <td rowspan="14" style="vertical-align: top;">
                <?php
                require_once 'gallery.php';
                ?>
            </td>
        </tr>
        <tr>
            <th>Tipologia</th>
            <td><?php echo $obj->tipologia->nome; ?></td>
        </tr>
        <tr>
            <th>Anno</th>
            <td><?php echo $obj->anno; ?></td>
        </tr>
        <tr>
            <th>Km</th>
            <td><?php echo $obj->km; ?></td>
        </tr>
        <tr>
            <th>Alimentazione</th>
            <td><?php echo $obj->alimentazione->nome; ?></td>
        </tr>
        <tr>
            <th>Cilindrata (cc)</th>
            <td><?php echo $obj->cc; ?></td>
        </tr>
        <tr>
            <th>Potenza (kw)</th>
            <td><?php echo $obj->kw; ?></td>
        </tr>
        <tr>
            <th>Colore</th>
            <td><?php echo $obj->colore; ?></td>
        </tr>
        <tr>
            <th>Portata totale (kg)</th>
            <td><?php echo $obj->portatatotale; ?></td>
        </tr>
        <tr>
            <th>Portata utile (kg)</th>
            <td><?php echo $obj->portatautile; ?></td>
        </tr>
        <tr>
            <th>Prezzo</th>
            <td><span style="font-size: 14px; font-weight: bold;"><?php echo isset($_SESSION['show_price']) && !$_SESSION['show_price'] ? '&nbsp;-' : '€ ' . $obj->prezzo; ?></span></td>
        </tr>
        <tr>
            <th colspan="2">Ulteriori informazioni</th>
        </tr>
        <tr>
            <td colspan="2" style="vertical-align: top;"><?php echo nl2br($obj->note); ?></td>
        </tr>
        <tr>
            <td colspan="2" style="vertical-align: top;">
                <span id="richiesta_info" style="text-decoration: underline; cursor: pointer;">Richiedi info</span> -
                <span id="segnala" style="text-decoration: underline; cursor: pointer;">Invia ad un amico</span>
                <?php if (isset($_SESSION['user']) && get_class($_SESSION['user']) == 'WebUser') { ?>
                    - <a id="edit" href="/admin/car.php?id=<?php echo $obj->Id; ?>">ADMIN EDIT</a>
                <?php } ?>
            </td>
        </tr>
    </tbody>
</table>
<div id="richiesta_info_div" title="Informazioni">
</div>

<script type="text/javascript">
    $(document).ready(function () {
        var ord = getParameterByName('sended');
        if (ord) {
            $('#richiesta_info_div').html('Sarete contattati il prima possibile, grazie per il vostro interese');
            $('#richiesta_info_div').dialog({
                autoOpen: true,
                resizable: false,
                show: {
                    effect: "highlight",
                    duration: 500
                },
                buttons: {
                    Ok: function () {
                        $(this).dialog("close");
                    }
                }
            });
        }

        $('#richiesta_info').click(function () {
            $('#richiesta_info_div').attr('title', 'Richiesta informazioni');
            loadEmailForm('info');
            return false;
        });

        $('#segnala').click(function () {
            $('#richiesta_info_div').attr('title', 'Segnala ad un amico');
            loadEmailForm('segnala');
            return false;
        });
    });

    var loadEmailForm = function (model) {
        var url = '/formcontattaci.php';
        if (model == 'segnala') {
            url = '/formsegnala.php'
        }

        $('#richiesta_info_div').load(url, function () {
            $('#richiesta_info_div table tfoot').remove();

            var $veicolo = $('<input/>').attr('id', 'veicolo').attr('name', 'veicolo').attr('type', 'hidden').val(<?php echo $obj->Id; ?>);
            $('#formcontattaci').append($veicolo);

            var $email_model = $('<input/>').attr('id', 'email_model').attr('name', 'email_model').attr('type', 'hidden').val(model);
            $('#formcontattaci').append($email_model);

            $('#richiesta_info_div').dialog({
                autoOpen: true,
                resizable: false,
                modal: true,
                width: 'auto',
                show: {
                    effect: "highlight",
                    duration: 500
                },
                buttons: {
                    Ok: function () {
                        var $invia = $('<input/>').attr('id', 'invia').attr('name', 'invia').attr('type', 'hidden').val('true');
                        $('#formcontattaci').append($invia);

                        $('#formcontattaci').submit();
                    },
                    Cancel: function () {
                        $(this).dialog("close");
                    }
                }
            });
        })
    };
</script>

<?php
require_once '_footer.php';
